package com.gvfish.core;

import java.util.HashMap;
import java.util.Map;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;

import org.springframework.beans.factory.FactoryBean;

public class MyCacheFactory  implements FactoryBean<Cache>{
	private static MyCacheFactory instance = null;
	private Cache cache = null;

	private MyCacheFactory() {

	}

	public static MyCacheFactory getInstance() {
		if (instance == null)
			instance = new MyCacheFactory();
		return instance;
	}

	public Cache getCache() {
		Map<String,Object> testMap=new HashMap<String, Object>();
		if (cache == null) {
			try {
				CacheFactory cacheFactory = CacheManager.getInstance()
						.getCacheFactory();
				cache = cacheFactory.createCache(testMap);
			} catch (CacheException e) {
				// ...
			}
		}
		return cache;
	}

	@Override
	public Cache getObject() throws Exception {
		return getCache();
	}

	@Override
	public Class<? extends Cache> getObjectType() {
		return Cache.class;
	}

	@Override
	public boolean isSingleton() {
		// TODO Auto-generated method stub
		return true;
	}
}
